package com.codinghero.core.bo;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.codinghero.core.dao.NameValDao;
import com.codinghero.core.dto.NameVal;

public class NameValBo {
	
	protected static Logger log = Logger.getLogger(NameValBo.class);
	
	private NameValDao nameValDao = new NameValDao();
	
//	public NameVal getNameValByName(String name) throws SQLException {
//		return nameValDao.getNameValByName(name);
//	}
//	
//	public int insertNameVal(NameVal nameVal) throws SQLException {
//		return nameValDao.insertNameVal(nameVal);
//	}
//	
//	public int updateNameVal(String name, String val) throws SQLException {
//		return nameValDao.updateNameVal(name, val);
//	}
	
	public int saveNameVal(String name, String val) {
		try {
			nameValDao.connect();
			NameVal nameVal = nameValDao.getNameValByName(name);
			log.info("new name:" + name + " val:" + val);
			if (nameVal == null || nameVal.getId() == null) {
				return nameValDao.insertNameVal(name, val);
			} else {
				log.info("old name:" + nameVal.getName() + " val:" + nameVal.getVal());
				return nameValDao.updateNameVal(name, val);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally {
			nameValDao.close();
		}
	}
}
